package com.test.thread.virtualthread;

import java.time.Duration;

/**
 * run need add vm option: --enable-preview --add-exports java.base/jdk.internal.vm=ALL-UNNAMED
 *
 * @link <a href="https://www.bilibili.com/video/BV1JaR5YSErG">virtual thread theory demo</a>
 */
public class Demo {

    public static final VirtualThreadScheduler SCHEDULER = new VirtualThreadScheduler();

    public void main() {
        new Thread(SCHEDULER::start).start();

        for (int i = 0; i < 1; i++) {
            var vt1 = new VirtualThread(() -> {
                System.out.println("1.1");
                System.out.println("1.2");
                WaitingOperation.perform("Network", Duration.ofSeconds(2));
                System.out.println("1.3");
                System.out.println("1.4");
            });
            var vt2 = new VirtualThread(() -> {
                System.out.println("2.1");
                System.out.println("2.2");
                WaitingOperation.perform("DB", Duration.ofSeconds(2));
                System.out.println("2.3");
                System.out.println("2.4");
            });
            //var vt3 = new VirtualThread(() -> {
            //    System.out.println("3.1");
            //    System.out.println("3.2");
            //    WaitingOperation.perform("IO", Duration.ofSeconds(2));
            //    System.out.println("3.3");
            //    System.out.println("3.4");
            //});
            //var vt4 = new VirtualThread(() -> {
            //    System.out.println("4.1");
            //    System.out.println("4.2");
            //    System.out.println("4.3");
            //    System.out.println("4.4");
            //});
            SCHEDULER.schedule(vt1);
            SCHEDULER.schedule(vt2);
            //SCHEDULER.schedule(vt3);
            //SCHEDULER.schedule(vt4);
        }

    }
}
